<?php

    // Created By : Julian Ponce
    // B.S. Computer Science - De La Salle Canlubang
    // November 26, 2011
    // IN parameters : routeId 
    // OUT parameters : List of route details including route step details.

// Start - Checker for those users who will just go to the page by typing directly in the url.
if ($_POST) {   
    // INCLUDES CONFIGURATION VARIABLES FOR DATABASE ACCESS
    include ("../includes/siteConfig.php");

    // CONNECT TO DATABASE
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');
    
    // GET VARIABLES VIA POST METHOD
    $routeId = $_POST['routeId'];
    
    // QUERY ROUTE STEP DETAILS OF PARTICULAR ROUTE ID
    $query = 
        "
            SELECT *
            FROM RouteStep
            WHERE routeId = '$routeId'
            ORDER BY subRouteId, stepNumber
        ";
    $result = mysql_query($query) or die ('Error in query: $queryDocumentSetupId. ' . mysql_error());

    
    // Checks if there is an attachment for this stepId
    if (mysql_num_rows($result) > 0) {
        while ($row = mysql_fetch_array($result)) {
            // SAVE TO TEMPORARY VARIABLE (subRouteId)
            $subRouteIdTemp = $row['subRouteId'];
            
            $jsondata = array();
            $jsondata['stepId'] = $row['stepId'];
            $jsondata['routeId'] = $row['routeId'];
            $jsondata['stepNumber'] = $row['stepNumber'];
            $jsondata['role'] = $row['role'];
            $jsondata['canBeCancelled'] = $row['canBeCancelled'];
            
            
            
            // CHECK IF SUB ROUTE ID IS NULL > MAKE subRouteName == "NULL"
            if (is_null($subRouteIdTemp)) {
                $jsondata['subRouteName'] = "NULL";
                $jsondata['subRouteId'] = "NULL";
            }
            
            // ELSE, GET SUB ROUTE NAME
            else {
                $querySubRouteName = 
                    "
                        SELECT *
                        FROM SubRoute
                        WHERE subRouteId = '$subRouteIdTemp'
                    ";
                $resultSubRouteName = mysql_query($querySubRouteName) or die ('Error in query: $queryDocumentSetupId. ' . mysql_error());
                
                $rowRouteName = mysql_fetch_array($resultSubRouteName);
                $jsondata['subRouteName'] = $rowRouteName['subRouteName'];
                $jsondata['subRouteId'] = $subRouteIdTemp;
            }
            
            $feed[] = $jsondata;

        }
        echo json_encode($feed);
    }
    else {
        echo json_encode(0);
    }
   
    mysql_free_result($result);
    mysql_close($connect);
}   // End - Checker for those users who will just go to the page by typing directly in the url.
else {
 echo "You are not authorized to view this page. This incident will be reported immediately.";
}
?>
